﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ivy.Common.Framework.GeneralUtility;
using Ivy.UserRegistration.Entity;
using Ivy.UserRegistration.IBusinessLogic;
using Ivy.UserRegistration.BusinessLogic;

using DasIdeen.ViewEntity;

namespace DasIdeen.Pages
{
    public partial class ForgetPassword : System.Web.UI.Page
    {
        #region  Variable & constant
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #endregion Public Methods

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        
        protected void btnEmailIdOk_Click(object sender, EventArgs e)
        {
            try
            {
                this.DivVerificationCode.Visible = true;
                User lUser = new User();
                lUser.UserPrimaryEmailID = this.txtEmailId.Text;
                UserRegistrationBus lUserRegistrationBus = new UserRegistrationBus();
                lUser = lUserRegistrationBus.GetUserByPrimaryEmailId(lUser.UserPrimaryEmailID);
                lUserRegistrationBus.SendVerificationCodeForForgetPasswordEmail(lUser);
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }
                
        protected void btnVerificationOk_Click(object sender, EventArgs e)
        {
            try
            {
                this.DivEmailId.Visible = false;
                this.DivVerificationCode.Visible = false;

                IUserRegistrationBus lUserRegistrationBus = new UserRegistrationBus();
                User lUser = new User();
                lUser.UserPrimaryEmailID = this.txtEmailId.Text;
                lUser = lUserRegistrationBus.GetUserByPrimaryEmailId(lUser.UserPrimaryEmailID);
                if (lUser.UserVerificationCode == this.txtVerifactionCode.Text.Trim())
                {
                    //lUser.UserIsVerified = true;
                    //lUserRegistrationBus.UpdateRegisteredUser(lUser);
                    this.DivNewPassword.Visible = true;

                }
                else
                {
                    this.lblMessage.ForeColor = System.Drawing.Color.Red;
                    this.lblMessage.Text = " Invailed Verification Code ";
                    this.DivEmailId.Visible = true;
                    this.DivVerificationCode.Visible = true;
                }
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }
        
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtNewPassword.Text == "" && this.txtRePassword.Text == "")
                {
                    this.lblErrorMessage.Text = "Can't be blank";
                    this.DivNewPassword.Visible = true;
                }
                else
                {
                    if (this.txtNewPassword.Text == this.txtRePassword.Text)
                    {
                        this.DivNewPassword.Visible = false;
                        UserContext UserSession = new UserContext();
                        UserRegistrationBus lUserRegistrationBus = new UserRegistrationBus();
                        User lUser = GetUserPrimaryEmailId(this.txtEmailId.Text.Trim());
                        lUser.UserPassword = StringUtility.EncryptString(this.txtNewPassword.Text);

                        lUserRegistrationBus.UpdateRegisteredUser(lUser);
                        UserSession.CurrentUser = lUser;
                        Session["UserContext"] = UserSession;


                        this.DivPasswordChengedMessage.Visible = true;
                    }
                    else
                    {
                        this.lblErrorMessage.ForeColor = System.Drawing.Color.Red;
                        this.lblErrorMessage.Text = "New password and Re-Entered password not matching";
                        this.DivNewPassword.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }

        }
        
        protected void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("../Default.aspx");
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }
        #endregion Events

        #region Private Methods
        private void CheckUserAutherization()
        {
        }

        private void ClearScreen()
        {
        }

        private void InitializeScreen()
        {
            ClearScreen();
            FillLoadLookUpData();
        }

        private void FillLoadLookUpData()
        {
        }

        private void TranslateEntityToUIControls()
        {
        }

        private void TranslateUIControlsToEntity()
        {
        }

        private User GetUserPrimaryEmailId(string UserPrimaryEmailId)
        {
            User lUser = new User();
            UserRegistrationBus lUserRegistrationBus = new UserRegistrationBus();
            lUser = lUserRegistrationBus.GetUserByPrimaryEmailId(UserPrimaryEmailId);
            return lUser;
        }
        #endregion Private Methods

    }
}